package com.bing.services;

import com.bing.Constant;
import com.bing.HUtils;
import com.bing.entity.Question;
import com.bing.entity.Status;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.List;
import java.util.Set;

/**
 * Created by kaibb on 15-11-1.
 */
public class QuestionsServicesImpl implements QuestionServices {



    @Override
    public void saveMysqlQuestions(List<Question> questions) {
        if (questions.size() == 0) {
            return;
        }
        Session session = HUtils.getMySqlSession();
        session.beginTransaction();
        for (Question question : questions) {
            session.save(question);
        }
        session.flush();
        session.clear();
        session.getTransaction().commit();
        session.close();
    }



    @Override
    public long getTotalQuestion() {
        Session session = HUtils.getMySqlSession();
        session.beginTransaction();
        Query query = session.createQuery("select count(*) from Question");
        Long result = (Long) query.uniqueResult();
        session.getTransaction().commit();
        session.close();
        return result;

    }

    @Override
    public void initQuestion() {
        Session session = HUtils.getMySqlSession();
        session.beginTransaction();
        for (int i = 1; i <= Constant.TOTAL_QUESTIONS; i++) {
            Question q = new Question();
            q.setQid(i);
            q.setStatus(Status.INIT);
            session.save(q);
            if (i % Constant.BATCH_SIZE == 0) {
                session.flush();
                session.clear();
            }
        }
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public void update(List<Question> questions) {
        Session session = HUtils.getMySqlSession();
        session.beginTransaction();

        for (Question question : questions) {
            session.update(question);
        }
        session.flush();
        session.clear();
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public List<Question> getNotOkQuestions(int size) {
        Session session = HUtils.getMySqlSession();
        session.beginTransaction();
        Query query;

        query = session.createQuery("from Question where status in (:status) order by id asc");
        query.setParameterList("status", new Status[]{Status.INIT, Status.TIMEOUT});
        query.setMaxResults(size);

        List list = query.list();
        session.flush();
        session.clear();
        session.getTransaction().commit();
        session.close();
        return list;
    }

    @Override
    public List<Question> getNoAidQuestions(int size) {
        Session session = HUtils.getMySqlSession();
        session.beginTransaction();
        Query query;

        query = session.createQuery("from Question where status=:status and aid=0");
        query.setParameter("status", Status.OK);
        query.setMaxResults(size);

        List list = query.list();
        session.flush();
        session.clear();
        session.getTransaction().commit();
        session.close();
        return list;
    }

    @Override
    public synchronized void updateIds(Set<Integer> question) {
        if (question == null || question.size() == 0) {
            return;
        }
        Session session = HUtils.getMySqlSession();
        session.beginTransaction();
        Question q;
        for (Integer integer : question) {
            Object result = session.createQuery("select q.id from Question q where q.qid=:qid").setParameter("qid", String.valueOf(integer)).uniqueResult();
            if (null == result) {
                q = new Question();
                q.setQid(integer);
                q.setStatus(Status.INIT);
                session.save(q);
            }
        }
        session.flush();
        session.clear();
        session.getTransaction().commit();
        session.close();
    }
}
